﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;

namespace Portal.CMS
{
    public class AddGroupLink : Portal.Interface.WebPart
    {
        private int MaxItems = 50;
        public override string WebPartId
        {
            get
            {
                return "AddGroupLink";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Nhóm liên kết");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddGroupLink));
            string belongSite = (string)Environments.Request("belongSite");
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "GroupLink.js") +
                             "<input id=txtBelongSite type=hidden value=\"" + belongSite + "\">" +
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "AddGroupLinkCaption", "Tên nhóm") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtGroupLink></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveAddGroupLink(); value=\"" + ResourceBLL.GetText(WebPartId, "AddGroupLinkSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "AddGroupLinkCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>" +
                             "      </div>" +
                             "      <div class=cssGroupLinkItems>" +
                             "      <table>" +
                             "          <tr>" +
                             "              <th class=cssItemName>" + ResourceBLL.GetText(WebPartId, "LinkItemName", "Tên liên kết") + "</th>" +
                             "              <th class=cssItemUrl>" + ResourceBLL.GetText(WebPartId, "LinkItemUrl", "Url") + "</th>" +
                             "              <th class=cssItemTarget>" + ResourceBLL.GetText(WebPartId, "LinkItemTarget", "target") + "</th>" +
                             "              <th class=cssItemDescription>" + ResourceBLL.GetText(WebPartId, "Description", "Note") + "</th>" +
                             "              <th class=cssItemIndexOrder>" + ResourceBLL.GetText(WebPartId, "LinkItemIndexOrder", "Thứ tự") + "</th>" +
                             "          </tr>";
                             for (int Index = 0; Index < MaxItems; Index++)
                             {
                                RetHtml += "<tr>" +
                                                "<td><input class=cssInputItemName id=\"txtItemName_" + Index + "\"></td>\r\n" +
                                                "<td><input class=cssInputItemUrl id=\"txtItemUrl_" + Index + "\"></td>\r\n" +
                                                "<td><input value=\"_blank\" class=cssInputItemTarget id=\"txtItemTarget_" + Index + "\"></td>\r\n" +
                                                "<td><input class=cssInputItemDescription id=\"txtItemDescription_" + Index + "\"></td>\r\n" +
                                                "<td><input class=cssInputItemIndexOrder value=\"" + (Index + 1) + "\" id=\"txtItemIndexOrder_" + Index + "\"></td>\r\n"+
                                           "</tr>";
                             }
                            RetHtml += "</table></div>";

                            
            RetHtml = Environment.Environments.GetFormDialog(RetHtml, ResourceBLL.GetText(WebPartId, "AddGroupLinkFormTitle", "Thêm mới nhóm liên kết"));
            return RetHtml;
        }

       

        [AjaxPro.AjaxMethod]
        public string ServerSideAddGroupLink(string belongSite,string GroupLinkName, GroupLinkItemCls[] GroupLinkItems)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                //PermissionTypeCls PermissionTypeCls = new GroupLink().FindPermissionType("GroupLinkItems");
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddGroupLinks", GroupLinkId, true);

                if (GroupLinkName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên liên kết"));
                string GroupLinkId = new GroupLinkBLL().AddGroupLink(null,belongSite, Environment.Environments.CurrentLoginUser.LoginName, GroupLinkName, GroupLinkItems);
                return ResourceBLL.GetText(WebPartId,"MessageAddOk","Thêm thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public GroupLinkItemCls[] GetGroupLinkItemsObject()
        {
            GroupLinkItemCls[] GroupLinkItems = new GroupLinkItemCls[MaxItems];
            for (int iIndex = 0; iIndex < GroupLinkItems.Length; iIndex++)
            {
                GroupLinkItems[iIndex] = new GroupLinkItemCls();
            }
            return GroupLinkItems;
        }

        [AjaxPro.AjaxMethod]
        public string GetControlName(string Name, int Index)
        {
            return Name + "_" + Index.ToString();
        }
    }
}